package com.atguigu.designpattern.singleton.type7;

public class SingletonTest07 {

    public static void main(String[] args) {
        System.out.println("单例模式-懒汉式-静态内部类（线程安全，懒加载，推荐！！）");
        Singleton singleton1 = Singleton.getInstance();
        Singleton singleton2 = Singleton.getInstance();

        System.out.println(singleton1 == singleton2);
        System.out.println("singleton1 hashcode" + singleton1.hashCode());
        System.out.println("singleton2 hashcode" + singleton2.hashCode());

    }

}

class Singleton{

    //1、构造器私有化，外部不能new
    private Singleton() {

    }

    //2、定义静态内部类，该类中有一个静态属性Singleton
    //jvm装载类是是线程安全的
    private static class SingletonInstance {
        private static final Singleton instance  = new Singleton();

    }

    // 提供一个静态的公共方法，直接返回静态内部类中的定义的属性
    public static Singleton getInstance() {
        return SingletonInstance.instance;
    }
}
